/*
 * 文件名：Config.java
 * 版权：卫士通移动事业部
 * 描述：读取配置文件
 * 创建人：陈振
 * 创建时间：2015.06.09
 */
package com.wct.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;

import org.yaml.snakeyaml.Yaml;

/**
 * 读取配置文件
 */
public class Config {
	private static Config config;
	public static String getPath(){  
		URL url = Config.class.getProtectionDomain().getCodeSource().getLocation();  
        String filePath = null;  
        try {  
            filePath = URLDecoder.decode(url.getPath(), "utf-8");// 转化为utf-8编码  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        if (filePath.endsWith(".jar")) {// 可执行jar包运行的结果里包含".jar"  
            // 截取路径中的jar包名  
            filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);  
        }  
          
        File file = new File(filePath);  
          
        // /If this abstract pathname is already absolute, then the pathname  
        // string is simply returned as if by the getPath method. If this  
        // abstract pathname is the empty abstract pathname then the pathname  
        // string of the current user directory, which is named by the system  
        // property user.dir, is returned.  
        filePath = file.getAbsolutePath();//得到windows下的正确路径  
        return filePath;  
    }  
	public static Config getInstance()
	{
		try {
	        if (config!=null) {
	            return config;
	        }
	        else {
	        	Yaml yaml = new Yaml();
	        	InputStream in;
	            try {
	                in = new FileInputStream(getPath()+"/config.yml" );
	                config = yaml.loadAs( in, Config.class );
	                return config;
	            } catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	                return null;
	            }
	        	
	        }
        } catch (Exception e) {
	        e.printStackTrace();
	        throw e;
        }
	}

	private SessionConfig session;
	private MemcacheConfig memcache;
	private DBConfig dbconfig;
	private JMSQueueConfig jmsQueue;
	private XinGeConfig xinge;

	public DBConfig getDbconfig() {
		return dbconfig;
	}
	public void setDbconfig(DBConfig dbconfig) {
		this.dbconfig = dbconfig;
	}
	public MemcacheConfig getMemcache() {
		return memcache;
	}
	public void setMemcache(MemcacheConfig memcache) {
		this.memcache = memcache;
	}
	public SessionConfig getSession() {
		return session;
	}
	public void setSession(SessionConfig session) {
		this.session = session;
	}
	public JMSQueueConfig getJmsQueue() {
		return jmsQueue;
	}
	public void setJmsQueue(JMSQueueConfig jmsQueue) {
		this.jmsQueue = jmsQueue;
	}
	public XinGeConfig getXinge() {
		return xinge;
	}
	public void setXinge(XinGeConfig xinge) {
		this.xinge = xinge;
	}
}
